﻿using drawing_print.comm;
using drawing_print.configure;
using drawing_print.util;
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace drawing_print.form
{
    public partial class schedule : Form
    {
        private string url;
        private string fileType;

        public schedule(string url, string fileType)
        {
            InitializeComponent();
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            this.url = url;
            this.fileType = fileType;
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            Task.Factory.StartNew(() =>
            {
                this.download(url, fileType);
            });
        }

        public void change_val(int value)
        {
            this.progressBar1.Value = value;
        }

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="url"></param>
        /// <param name="fileType"></param>
        public void download(string url, string fileType)
        {
            // 文件路径
            string downloadFile = Path.Combine(AppConfig.FiliPath, "SourceFile" + fileType);
            // 下载文件路径
            AppConfig.SourceFilePath = downloadFile;
            // 下载文件扩展名
            AppConfig.FileExtension = fileType;
            this.progressBar1.Value = 40;
            this.label3.Text = this.progressBar1.Value.ToString() + "%";
            this.label1.Text = "正在下载文件...";
            // 下载文件
            if (!Download.download(url, AppConfig.SourceFilePath))
            {
                    MessageBox.Show("文件下载失败!");
                    this.Close();
                    AppConfig.isWork = false;
                    return;
                
            }
            else
            {
                this.progressBar1.Value = 60;
                this.label3.Text = this.progressBar1.Value.ToString() + "%";
                this.label1.Text = "下载成功";
                AppConfig.log.log("--------文件下载成功--------");
                this.converType();
                // 下载完成

            }
        }

        public void converType()
        {
            this.label1.Text = "正在解析文件...";
            this.progressBar1.Value = 70;
            this.label3.Text = this.progressBar1.Value.ToString() + "%";
            bool res = FileConver.allToPdf(AppConfig.SourceFilePath, AppConfig.TargetFilePath);
            if (!res)
            {
                this.Close();
                MessageBox.Show("文件解析失败。");
                AppConfig.isWork = false;
                return;
            }
            this.progressBar1.Value = 80;
            this.label3.Text = this.progressBar1.Value.ToString() + "%";
            this.label1.Text = "文件解析成功。";
            AppConfig.log.log("--------文件转换PDF--------");

            this.toImage();
        }

        public void toImage()
        {
            this.label1.Text = "正在转换文件...";
            this.progressBar1.Value = 90;
            this.label3.Text = this.progressBar1.Value.ToString() + "%";
            bool res = Pdf.PdfToImage(AppConfig.TargetFilePath);
            this.progressBar1.Value = 100;
            this.label3.Text = this.progressBar1.Value.ToString() + "%";
            this.Close();
            if (!res)
            {
                AppConfig.log.log("文件转换失败。");
                MessageBox.Show("文件转换失败。");
                AppConfig.isWork = false;
                return;
            }
            
            AppConfig.log.log("文件转换PDF成功。");
            //Application.Run(new Form1());
            var form = new Form1();
            form.ShowDialog();
            
        }
    }
}
